sap.ui.define([
	"sap/ui/core/mvc/Controller",
	"sap/ui/core/routing/History",
	"sap/m/MessageBox"
], function(Controller, History, MessageBox) {
	"use strict";

	return Controller.extend("com.wp.SWIFTSAP.controller.BaseController", {
		/**
		 * Convenience method for accessing the router in every controller of the application.
		 * @public
		 * @returns {sap.ui.core.routing.Router} the router for this component
		 */
		getRouter: function() {
			return this.getOwnerComponent().getRouter();
		},

		/**
		 * Convenience method for getting the view model by name in every controller of the application.
		 * @public
		 * @param {string} sName the model name
		 * @returns {sap.ui.model.Model} the model instance
		 */
		getModel: function(sName) {
			return this.getView().getModel(sName);
		},

		/**
		 * Convenience method for setting the view model in every controller of the application.
		 * @public
		 * @param {sap.ui.model.Model} oModel the model instance
		 * @param {string} sName the model name
		 * @returns {sap.ui.mvc.View} the view instance
		 */
		setModel: function(oModel, sName) {
			return this.getView().setModel(oModel, sName);
		},

		/**
		 * Convenience method for getting the resource bundle.
		 * @public
		 * @returns {sap.ui.model.resource.ResourceModel} the resourceModel of the component
		 */
		getResourceBundle: function() {
			return this.getOwnerComponent().getModel("i18n").getResourceBundle();
		},

		/**
		 * Event handler for navigating back.
		 * It there is a history entry we go one step back in the browser history
		 * If not, it will replace the current entry of the browser history with the inbox route.
		 * @public
		 */
		onNavBack: function() {
			var sPreviousHash = History.getInstance().getPreviousHash();

			if (sPreviousHash !== undefined) {
				// eslint-disable-next-line sap-no-history-manipulation
				history.go(-1);
			} else {
				this.getRouter().navTo("inbox", {}, true);
			}
		},

		logout: function() {
			var oThis = this;

			$.ajax({
				type: "GET",
				url: "/sap/public/bc/icf/logoff" //Clear SSO cookies: SAP Provided service to do that  
			}).done(function(data) { //Now clear the authentication header stored in the browser  
				if (!document.execCommand("ClearAuthenticationCache")) {
					//"ClearAuthenticationCache" will work only for IE. Below code for other browsers  
					$.ajax({
						type: "GET",
						url: "/sap/public/bc/icf/logoff", //any URL to a Gateway service  
						username: '', //dummy credentials: when request fails, will clear the authentication header  
						password: '',
						statusCode: {
							401: function() {
								//This empty handler function will prevent authentication pop-up in chrome/firefox  
							}
						},
						error: function() {
							oThis.showErrorMessage(oThis.getResourceBundle().getText("userLogoutFail"));
						},
						success: function() {
							var sUrl = "index.html";
							sap.m.URLHelper.redirect(sUrl, false);
						}
					});
				}
			});

		},

		/*		setLanguage: function(evt) {
					var btnID = evt.getSource().getId();
					var langu = sap.ui.getCore().getConfiguration().getLanguage();

					if (btnID.indexOf("btnLanguZH") !== -1) {
						if (langu !== "zh") {
							sap.ui.getCore().getConfiguration().setLanguage("zh");
							MessageBox.show("zh");
						}
					} else if (btnID.indexOf("btnLanguEN") !== -1) {
						if (langu !== "en") {
							sap.ui.getCore().getConfiguration().setLanguage("en");
							MessageBox.show("en");
						}
					} else {
						MessageBox.show("Not Suppport Language");
					}

				},*/

		showErrorMessage: function(sMsg) {
			MessageBox.error(sMsg, {
				styleClass: this.getOwnerComponent().getContentDensityClass()
			});
		},

		showSuccessMessage: function(sMsg, oFunction) {
			MessageBox.success(sMsg, {
				styleClass: this.getOwnerComponent().getContentDensityClass(),
				onClose: oFunction
			});
		},
		loadInitData: function(othis, oViewModel) {
			// 获取用户信息
			oViewModel.setProperty("/busy", true);
			var oComponentModel = othis.getOwnerComponent().getModel("user");
			var sObjectPath01 = othis.getModel().createKey("/UserSet", {
				Userid: "###defaultsapuser###" // 沙箱测试环境用此  
			});
			//语言设置
			var vLanguage = sap.ui.getCore().getConfiguration().getLanguage();
			if (vLanguage === "") {
				vLanguage = "zh";
			}
			vLanguage = vLanguage.toLowerCase();
			oComponentModel.setProperty("/Language", vLanguage);

			//能获取前台登陆用户 
			//var userName = sap.ushell.Container.getService("UserInfo").getId();
			//var modeluser = othis.getOwnerComponent().getModel("userapi");
			//MessageBox.show(modeluser.oData.name + "test AAA");

			othis.getModel().read(sObjectPath01, {
				success: function(oData) {
					//多语言支持 
					if (vLanguage.indexOf("zh") !== -1) {
						oData.Name3 = oData.Name1;
					} else if (vLanguage.indexOf("en") !== -1) {
						oData.Name3 = oData.Name2;
					}
					if (oData.Subcategories !== "") {
						oData.Subcategories = JSON.parse(oData.Subcategories);
						//多语言支持 
						var i = oData.Subcategories.length;
						while (i--) {
							if (vLanguage.indexOf("zh") !== -1) {
								oData.Subcategories[i].SUBCATEGORY3 = oData.Subcategories[i].SUBCATEGORY1;
							} else if (vLanguage.indexOf("en") !== -1) {
								oData.Subcategories[i].SUBCATEGORY3 = oData.Subcategories[i].SUBCATEGORY2;
							}
							//删除为空的元素
							if (oData.Subcategories[i].SUBCATEGORY3 === "") {
								oData.Subcategories.splice(i, 1);
							}
						}
					}

					oComponentModel.setProperty("/Userid", oData.Userid);
					oComponentModel.setProperty("/Name3", oData.Name3);
					oComponentModel.setProperty("/Sapid", oData.Sapid);
					oComponentModel.setProperty("/Email", oData.Email);
					oComponentModel.setProperty("/Tel", oData.Tel);
					oComponentModel.setProperty("/Subcategories", oData.Subcategories);
					oComponentModel.setProperty("/Swiftsaphost", oData.Swiftsaphost);

					var iOriginalBusyDelay = othis.getView().getBusyIndicatorDelay();
					oViewModel.setProperty("/busy", false);
					oViewModel.setProperty("/delay", iOriginalBusyDelay);

					if (oData.Inactive === "X" || oData.Inactive === "x") {
						othis.showErrorMessage(othis.getResourceBundle().getText("userNotActiveError"));
					} else {
						var currentHash = othis.getRouter().getHashChanger().getHash();
						//	var name = othis.getRouter().getRouteInfoByHash(currentHash); // since 1.75
						//	var currenRouter = othis.getRouter().getRoute(name);
						if (currentHash.indexOf("OutboxDetail/") !== -1) {
							othis.getRouter().navTo("outbox", {
								Userid: oData.Userid
							});
						} else if (currentHash.indexOf("InboxDetail/") !== -1 || !currentHash) {
							othis.getRouter().navTo("inbox", {
								Userid: oData.Userid
							});
						}
					}
				},

				error: function(oData) {
					othis.showErrorMessage(othis.getResourceBundle().getText("userNotExistError"));
					oViewModel.setProperty("/busy", false);
				}
			});
		}

	});

});